package com.github.dote.mio.test;


import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * 给java文件批量添加License信息
 * @author tanghc
 *
 */
public class Copyright {

    private static String lineSeperator = System.getProperty("line.separator");
    private static String encode = "UTF-8";

    private static String folder;
    private static String copyright;

    public Copyright(){

    }


    @Test
    public  void test() throws IOException {
        // 从文件读取版权内容
        // 在D盘创建一个copyright.txt文件,把版权内容放进去即可
        String copyright = readCopyrightFromFile("E:\\Dev\\Projects\\J\\m-io\\LICENSE");
        // 存放java文件的文件夹,必须是文件夹
        String folder = "E:\\Dev\\Projects\\J\\m-io\\src\\main\\java\\com\\github\\dote\\mio\\filter";

        this.folder=folder;
        this.copyright=copyright;
        new Copyright().process();

        System.out.println("end.");
    }

    public void process() {
        this.addCopyright(new File(folder));
    }

    private void addCopyright(File folder) {
        File[] files = folder.listFiles();

        if (files == null || files.length == 0) {
            return;
        }

        for (File f : files) {
            if (f.isFile()) {
                doAddCopyright(f);
            } else {
                addCopyright(f);
            }
        }
    }

    private void doAddCopyright(File file) {
        String fileName = file.getName();
        boolean isJavaFile = fileName.toLowerCase().endsWith(".java");
        if(isJavaFile) {
            try {
                this.doWrite(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void doWrite(File file) throws IOException {


        StringBuilder javaFileContent = new StringBuilder();
        String line = null;
        // 先添加copyright到文件头
        javaFileContent.append(copyright).append(lineSeperator);
        // 追加剩余内容
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encode));
        while ((line = br.readLine()) != null) {
            javaFileContent.append(line).append(lineSeperator);
        }

        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), encode);
        writer.write(javaFileContent.toString());
        writer.close();
        br.close();
    }

    private static String readCopyrightFromFile(String copyFilePath) throws IOException {
        StringBuilder copyright = new StringBuilder();

        String line = null;

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(copyFilePath), encode));

        while ((line = br.readLine()) != null) {
            copyright.append(line).append(lineSeperator);
        }
        br.close();

        return copyright.toString();
    }

}
